package o;

import java.io.EOFException;
import java.io.IOException;
import o.aea.a;

final class afl implements afn {
    private long ʻ;
    private long ʼ;
    private long ʽ;
    private final afq ˊ;
    private long ˊॱ;
    private final afp ˋ = new afp();
    private final long ˎ;
    private final long ˏ;
    private int ॱ;
    private long ॱˊ;
    private long ॱॱ;
    private long ᐝ;

    class d implements aea {
        final /* synthetic */ afl ˏ;

        private d(afl o_afl) {
            this.ˏ = o_afl;
        }

        public boolean ˊ() {
            return true;
        }

        public a ˏ(long j) {
            if (j == 0) {
                return new a(new aeh(0, this.ˏ.ˏ));
            }
            return new a(new aeh(j, this.ˏ.ॱ(this.ˏ.ˏ, this.ˏ.ˊ.ˎ(j), 30000)));
        }

        public long g_() {
            return this.ˏ.ˊ.ˏ(this.ˏ.ʼ);
        }
    }

    public /* synthetic */ aea ˏ() {
        return ˎ();
    }

    public afl(long j, long j2, afq o_afq, long j3, long j4, boolean z) {
        boolean z2 = j >= 0 && j2 > j;
        amm.ˊ(z2);
        this.ˊ = o_afq;
        this.ˏ = j;
        this.ˎ = j2;
        if (j3 == j2 - j || z) {
            this.ʼ = j4;
            this.ॱ = 3;
            return;
        }
        this.ॱ = 0;
    }

    public long ˋ(adv o_adv) throws IOException, InterruptedException {
        long j = 0;
        switch (this.ॱ) {
            case 0:
                this.ʻ = o_adv.ˊ();
                this.ॱ = 1;
                j = this.ˎ - 65307;
                if (j > this.ʻ) {
                    return j;
                }
                break;
            case 1:
                break;
            case 2:
                if (this.ᐝ != 0) {
                    long ˋ = ˋ(this.ᐝ, o_adv);
                    if (ˋ >= 0) {
                        return ˋ;
                    }
                    adv o_adv2 = o_adv;
                    j = ˊ(o_adv2, this.ᐝ, -(ˋ + 2));
                }
                this.ॱ = 3;
                return -(j + 2);
            case 3:
                return -1;
            default:
                throw new IllegalStateException();
        }
        this.ʼ = ॱ(o_adv);
        this.ॱ = 3;
        return this.ʻ;
    }

    public long ॱ(long j) {
        boolean z = this.ॱ == 3 || this.ॱ == 2;
        amm.ˊ(z);
        this.ᐝ = j == 0 ? 0 : this.ˊ.ˎ(j);
        this.ॱ = 2;
        ॱ();
        return this.ᐝ;
    }

    public d ˎ() {
        return this.ʼ != 0 ? new d() : null;
    }

    public void ॱ() {
        this.ʽ = this.ˏ;
        this.ॱॱ = this.ˎ;
        this.ॱˊ = 0;
        this.ˊॱ = this.ʼ;
    }

    public long ˋ(long j, adv o_adv) throws IOException, InterruptedException {
        if (this.ʽ == this.ॱॱ) {
            return -(this.ॱˊ + 2);
        }
        long ˊ = o_adv.ˊ();
        if (ॱ(o_adv, this.ॱॱ)) {
            this.ˋ.ˊ(o_adv, false);
            o_adv.ˏ();
            long j2 = j - this.ˋ.ˏ;
            int i = this.ˋ.ʻ + this.ˋ.ᐝ;
            if (j2 < 0 || j2 > 72000) {
                if (j2 < 0) {
                    this.ॱॱ = ˊ;
                    this.ˊॱ = this.ˋ.ˏ;
                } else {
                    this.ʽ = o_adv.ˊ() + ((long) i);
                    this.ॱˊ = this.ˋ.ˏ;
                    if ((this.ॱॱ - this.ʽ) + ((long) i) < 100000) {
                        o_adv.ॱ(i);
                        return -(this.ॱˊ + 2);
                    }
                }
                if (this.ॱॱ - this.ʽ < 100000) {
                    this.ॱॱ = this.ʽ;
                    return this.ʽ;
                }
                return Math.min(Math.max((o_adv.ˊ() - ((j2 <= 0 ? 2 : 1) * ((long) i))) + ((j2 * (this.ॱॱ - this.ʽ)) / (this.ˊॱ - this.ॱˊ)), this.ʽ), this.ॱॱ - 1);
            }
            o_adv.ॱ(i);
            return -(this.ˋ.ˏ + 2);
        } else if (this.ʽ != ˊ) {
            return this.ʽ;
        } else {
            throw new IOException("No ogg page can be found.");
        }
    }

    private long ॱ(long j, long j2, long j3) {
        long j4 = ((((this.ˎ - this.ˏ) * j2) / this.ʼ) - j3) + j;
        if (j4 < this.ˏ) {
            j4 = this.ˏ;
        }
        if (j4 >= this.ˎ) {
            return this.ˎ - 1;
        }
        return j4;
    }

    void ˎ(adv o_adv) throws IOException, InterruptedException {
        if (!ॱ(o_adv, this.ˎ)) {
            throw new EOFException();
        }
    }

    boolean ॱ(adv o_adv, long j) throws IOException, InterruptedException {
        long min = Math.min(3 + j, this.ˎ);
        byte[] bArr = new byte[2048];
        int length = bArr.length;
        while (true) {
            if (o_adv.ˊ() + ((long) length) > min) {
                length = (int) (min - o_adv.ˊ());
                if (length < 4) {
                    return false;
                }
            }
            o_adv.ˎ(bArr, 0, length, false);
            int i = 0;
            while (i < length - 3) {
                if (bArr[i] == (byte) 79 && bArr[i + 1] == (byte) 103 && bArr[i + 2] == (byte) 103 && bArr[i + 3] == (byte) 83) {
                    o_adv.ॱ(i);
                    return true;
                }
                i++;
            }
            o_adv.ॱ(length - 3);
        }
    }

    long ॱ(adv o_adv) throws IOException, InterruptedException {
        ˎ(o_adv);
        this.ˋ.ˊ();
        while ((this.ˋ.ˊ & 4) != 4 && o_adv.ˊ() < this.ˎ) {
            this.ˋ.ˊ(o_adv, false);
            o_adv.ॱ(this.ˋ.ʻ + this.ˋ.ᐝ);
        }
        return this.ˋ.ˏ;
    }

    long ˊ(adv o_adv, long j, long j2) throws IOException, InterruptedException {
        this.ˋ.ˊ(o_adv, false);
        while (this.ˋ.ˏ < j) {
            o_adv.ॱ(this.ˋ.ʻ + this.ˋ.ᐝ);
            j2 = this.ˋ.ˏ;
            this.ˋ.ˊ(o_adv, false);
        }
        o_adv.ˏ();
        return j2;
    }
}
